package com.atguigu.common.valid;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Set;


public class ListValConstraintValidator implements ConstraintValidator<ListVal, Integer> {


    private Set<Integer> set = new HashSet<>();

    //初始化方法
    @Override
    public void initialize(ListVal constraintAnnotation) {
        int[] values = constraintAnnotation.values();
        Arrays.stream(values).forEach(x -> {
            set.add(x);
        });
    }

    //判断是否校验成功
    @Override
    public boolean isValid(Integer value, ConstraintValidatorContext context) {
        return set.contains(value);
    }
}
